package trastienda.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.util.ArrayList;
//import java.util.Collection;

import trastienda.excepcion.DAOExcepcion;
import trastienda.modelo.Instrumentos;
import trastienda.util.ConexionBD;

public class InstrumentosDAO extends BaseDAO{
	
	public Instrumentos insertar(Instrumentos ins) throws DAOExcepcion {
		
		String query = "INSERT INTO Instrumentos(tipo, marca, modelo, cantidad, caracteristicas, precio, idArmado) VALUES (?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, ins.gettipo());
			stmt.setString(2, ins.getmarca());
			stmt.setString(3, ins.getmodelo());
			stmt.setInt(4, ins.getcantidad());
			stmt.setString(5, ins.getcaracteristicas());
			stmt.setDouble(6, ins.getprecio());
			stmt.setInt(7, ins.getidArmado());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
						
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ins;
	}
	public Instrumentos obtener(int idInstrumento) throws DAOExcepcion {
		Instrumentos ins = new Instrumentos();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select idInstrumento, tipo, marca, modelo,cantidad, caracteristicas, precio, idArmado from Instrumentos where idInstrumento=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idInstrumento);
			rs = stmt.executeQuery();
			if (rs.next()) {
				ins.setidInstrumento(rs.getInt(1));
				ins.settipo(rs.getString(2));
				ins.setmarca(rs.getString(3));
				ins.setmodelo(rs.getString(4));
				ins.setcantidad(rs.getInt(5));
				ins.setcaracteristicas(rs.getString(6));
				ins.setprecio(rs.getDouble(7));
				ins.setidArmado(rs.getInt(8));
				
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ins;
	}

	
	
	
	
	
}
